home *** CD-ROM | disk | FTP | other *** search
/ The Atari Compendium / The Atari Compendium (Toad Computers) (1994).iso / files / prgtools / programm.ing / libscsi1.zoo / LibScsi-0.01 / tape.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-03-14  |  3.0 KB  |  121 lines

  1. /*
  2.  * tape.c - Copyright Steve Woodford, October 1993
  3.  *
  4.  * Provide interface for backup/restore to SCSI tapedrive.
  5.  * This file is supplied as an example use of the scsi_io interface.
  6.  * It is lifted directly from my backup/restore program which I will
  7.  * tidy up one day to release to the public ;-).
  8.  *
  9.  * This is not standalone, it simply provides entry points used by
  10.  * my backup program. The functions are only of use as examples.
  11.  * I havn't bundled the two last include files 'backup.h' & 'bdev.h'
  12.  * since they are not needed to understand what's going on.
  13.  */
  14.  
  15. #include <stdlib.h>
  16. #include <errno.h>
  17. #include <sys/types.h>
  18. #include <sys/scsi_io.h>
  19. #include "backup.h"
  20. #include "bdev.h"
  21.  
  22. PRIVATE int      tape_fd;
  23.  
  24. PRIVATE char    *sense_msgs[] = SENSE_KEY_INFO;
  25.  
  26.  
  27. PUBLIC  int
  28. Bdev_Tape_Open(u_short drv, char *id)
  29. {
  30.     u_short  tid  = (*id == '\0') ? SCSI_TAPE_ID : (u_short)atoi(id);
  31.     char    *mode = Backing_Up ? "wa" : "ra";
  32.     Sc_Desc  sc;
  33.     char     tmp[32];
  34.     char     vend[20], prod[20], rev[20];
  35.  
  36.     if ( (tape_fd = scsi_open(tid, 0, mode)) < 0 ) {
  37.         Log_Error(1, "Can't open Scsi Tape:");
  38.         return(0);
  39.     }
  40.  
  41.     if ( scsi_ioctl(tape_fd, SCIO_GET, (long)(&sc)) != 0 ) {
  42.         Log_Error(1, "Can't ioctl tapedrive:");
  43.         (void)scsi_close(tape_fd);
  44.         tape_fd = -1;
  45.         return(0);
  46.     }
  47.  
  48.     if ( sc.sc_lun )
  49.         Sprintf(tmp, "Id:%d, Lun: %d", sc.sc_id, sc.sc_lun);
  50.     else
  51.         Sprintf(tmp, "Id:%d", sc.sc_id);
  52.  
  53.     Strncpy(vend, sc.sc_vendor, sizeof(sc.sc_vendor));
  54.     vend[sizeof(sc.sc_vendor)]   = '\0';
  55.     Strncpy(prod, sc.sc_product, sizeof(sc.sc_product));
  56.     prod[sizeof(sc.sc_product)]  = '\0';
  57.     Strncpy(rev, sc.sc_revision, sizeof(sc.sc_revision));
  58.     rev [sizeof(sc.sc_revision)] = '\0';
  59.  
  60.     Log_Message(MSG_INFO,
  61.                 "Scsi-Tape (%s): Vendor: %s, Product: %s, Revision: %s\n",
  62.                 tmp, vend, prod, rev);
  63.  
  64.     return(1);
  65. }
  66.  
  67.  
  68. PUBLIC  int
  69. Bdev_Tape_Close(void)
  70. {
  71.     (void) scsi_close(tape_fd);
  72.  
  73.     return(0);
  74. }
  75.  
  76.  
  77. PUBLIC  long
  78. Bdev_Tape_Read(char *buf, long bytes)
  79. {
  80.     long    nrd = scsi_read(tape_fd, buf, (u_long)bytes);
  81.  
  82.     if ( nrd <= 0 ) {
  83.         Sc_Desc sc;
  84.         u_char  key;
  85.  
  86.         if ( scsi_ioctl(tape_fd, SCIO_GET, (long)(&sc)) != 0 )
  87.             return(-1);
  88.  
  89.         key = sc.sc_sense_key;
  90.  
  91.         if ( (key != SK_NO_SENSE) &&  (key != SK_EOF) )
  92.             Log_Message(MSG_ERR, "Error reading tape. Code %d: %s (Errs: %d)",
  93.                                   key, sense_msgs[key], sc.sc_sense_blk);
  94.         return(-1);
  95.     }
  96.     return(nrd);
  97. }
  98.  
  99.  
  100. PUBLIC  long
  101. Bdev_Tape_Write(char *buf, long bytes)
  102. {
  103.     long    nwr = scsi_write(tape_fd, buf, (u_long)bytes);
  104.  
  105.     if ( nwr <= 0 ) {
  106.         Sc_Desc sc;
  107.         u_char  key;
  108.  
  109.         if ( scsi_ioctl(tape_fd, SCIO_GET, (long)(&sc)) != 0 )
  110.             return(-1);
  111.  
  112.         key = sc.sc_sense_key;
  113.  
  114.         if ( (key != SK_NO_SENSE) &&  (key != SK_EOF) )
  115.             Log_Message(MSG_ERR, "Error writing tape. Code %d: %s (Errs: %d)",
  116.                                   key, sense_msgs[key], sc.sc_sense_blk);
  117.         return(-1);
  118.     }
  119.     return(nwr);
  120. }
  121.